
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <h2><a name="t0"></a><a id="_0"></a>我的解题思路与分析</h2> 
<p>在 <strong>HarmonyOS 应用开发者高级认证考试</strong> 时，我遇到了这道题目。它是一道经典的<a href="https://so.csdn.net/so/search?q=%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%84%E7%90%86&amp;spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https://so.csdn.net/so/search?q=%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%A4%84%E7%90%86&amp;spm=1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;searchword\&quot;:\&quot;字符串处理\&quot;}&quot;}" data-tit="字符串处理" data-pretit="字符串处理">字符串处理</a>问题，要求我们从给定字符串中找出 <strong>最长的元音子串长度</strong>。当时我的第一反应是，这题考察的是对字符串连续性问题的处理能力以及代码的优化设计。以下是我在考试中的完整思路和解题过程。</p> 
<h3><a name="t1"></a><a id="_3"></a>理解题目需求</h3> 
<p><img src="https://i-blog.csdnimg.cn/direct/f995117849454feab7a3d7894804abe3.png#pic_center" alt="在这里插入图片描述"></p> 
<p>拿到题目后，我先快速梳理了一下要求，并明确了以下关键点：</p> 
<ol><li><strong>元音字符</strong>：包括 <code style="user-select: auto;">a, e, i, o, u</code> 和它们的大写版本 <code style="user-select: auto;">A, E, I, O, U</code>。</li><li><strong>子串的定义</strong>：子串必须是连续的字符序列，这意味着如果中间出现非元音字符，连续性就会被打断。</li><li><strong>性能要求</strong>：字符串长度可能达到 65,535，这是一个非常大的数据规模。因此，算法必须具有较低的时间复杂度，最好在一次遍历中完成。</li></ol> 
<p>基于这些分析，我迅速确定了一个基本思路：采用 <strong>线性遍历（O(n))</strong> 来处理这个问题，这样可以在高效完成任务的同时保证代码逻辑的简洁性。</p> 
<h3><a name="t2"></a><a id="_12"></a>设计解法</h3> 
<p>在设计<span class="edu-hl hl hl-1" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;extra&quot;:&quot;{\&quot;word\&quot;:\&quot;算法\&quot;}&quot;}" data-tit="算法" data-pretit="算法">算法</span>时，我的主要思路是动态记录 <strong>当前连续元音长度</strong> 和 <strong>最长元音长度</strong>：</p> 
<ol><li><strong>维护两个变量</strong>： 
  <ul><li><code style="user-select: auto;">currentLength</code>：用于记录当前连续的元音子串长度。</li><li><code style="user-select: auto;">maxLength</code>：用于存储在遍历过程中发现的最长元音子串长度。</li></ul> </li><li><strong>判断字符是否为元音</strong>： 
  <ul><li>使用一个 <code style="user-select: auto;">Set</code> 数据结构存储所有元音字符。相比数组，<code style="user-select: auto;">Set</code> 的查找效率更高（时间复杂度 <span class="katex--inline"><span class="katex"><span class="katex-mathml"> 
        
         
          
          
            O 
           
          
            ( 
           
          
            1 
           
          
            ) 
           
          
         
           O(1) 
          
         
       </span><span class="katex-html"><span class="base"><span class="strut" style="height: 1em; vertical-align: -0.25em;"></span><span class="mord mathnormal" style="margin-right: 0.0278em;">O</span><span class="mopen">(</span><span class="mord">1</span><span class="mclose">)</span></span></span></span></span>），可以更快速判断一个字符是否为元音。</li></ul> </li><li><strong>遍历逻辑</strong>： 
  <ul><li>遍历字符串中的每一个字符： 
    <ul><li>如果字符是元音，则将 <code style="user-select: auto;">currentLength</code> 加 1；</li><li>如果字符不是元音，则更新 <code style="user-select: auto;">maxLength</code>，并将 <code style="user-select: auto;">currentLength</code> 重置为 0。</li></ul> </li></ul> </li><li><strong>返回结果</strong>： 
  <ul><li>遍历结束后，<code style="user-select: auto;">maxLength</code> 中存储的就是字符串中的最长元音子串长度。</li></ul> </li></ol> 
<h3><a name="t3"></a><a id="_26"></a>代码实现</h3> 
<p>以下是我在考试中编写的代码：</p> 
<pre data-index="0" class="set-code-show prettyprint" name="code" style="user-select: auto;"><code class="prism language-javascript has-numbering" onclick="mdcp.copyCode(event)" style="position: unset; user-select: auto;"><span class="token keyword">function</span> <span class="token function">doFunc</span><span class="token punctuation">(</span><span class="token parameter">s</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    <span class="token keyword">let</span> maxLength <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 记录最长元音子串的长度</span>
    <span class="token keyword">let</span> currentLength <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 当前连续元音子串的长度</span>
    <span class="token keyword">const</span> vowels <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Set</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">,</span> <span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token string">'u'</span><span class="token punctuation">,</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token string">'E'</span><span class="token punctuation">,</span> <span class="token string">'I'</span><span class="token punctuation">,</span> <span class="token string">'O'</span><span class="token punctuation">,</span> <span class="token string">'U'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 定义元音集合</span>

    <span class="token comment">// 遍历字符串</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> s<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>vowels<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> 
            <span class="token comment">// 当前字符是元音</span>
            currentLength<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token comment">// 增加当前连续元音长度</span>
            maxLength <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>maxLength<span class="token punctuation">,</span> currentLength<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 更新最大值</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{<!-- --></span>
            <span class="token comment">// 当前字符不是元音</span>
            currentLength <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token comment">// 重置当前长度</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> maxLength<span class="token punctuation">;</span> <span class="token comment">// 返回最长元音子串长度</span>
<span class="token punctuation">}</span>
<div class="hljs-button {2} add_def" data-title="复制"></div></code><ul class="pre-numbering" style="opacity: 0.78561;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul><button class="btn-code-notes mdeditor" data-report-click="{&quot;spm&quot;:&quot;3001.10436&quot;,&quot;extra&quot;:{&quot;index&quot;:0,&quot;runIdx&quot;:-1}}">代码解读</button></pre> 
<h3><a name="t4"></a><a id="_49"></a>输入与输出</h3> 
<p>考试中，题目要求我们通过标准输入输出完成交互，因此我补充了以下代码来读取输入并输出结果：</p> 
<pre data-index="1" class="set-code-show prettyprint" name="code" style="user-select: auto;"><code class="prism language-javascript has-numbering" onclick="mdcp.copyCode(event)" style="position: unset; user-select: auto;">process<span class="token punctuation">.</span>stdin<span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
process<span class="token punctuation">.</span>stdin<span class="token punctuation">.</span><span class="token function">setEncoding</span><span class="token punctuation">(</span><span class="token string">"utf8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> input <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span>

process<span class="token punctuation">.</span>stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">"data"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{<!-- --></span>
    input <span class="token operator">+=</span> data<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

process<span class="token punctuation">.</span>stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">"end"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{<!-- --></span>
    <span class="token keyword">let</span> inputArray <span class="token operator">=</span> input<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> inputString <span class="token operator">=</span> inputArray<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">doFunc</span><span class="token punctuation">(</span>inputString<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 调用核心函数</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 输出结果</span>
    process<span class="token punctuation">.</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<div class="hljs-button {2} add_def" data-title="复制"></div></code><ul class="pre-numbering" style="opacity: 0.78561;"><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul><button class="btn-code-notes mdeditor" data-report-click="{&quot;spm&quot;:&quot;3001.10436&quot;,&quot;extra&quot;:{&quot;index&quot;:1,&quot;runIdx&quot;:-1}}">代码解读</button></pre> 
<h3><a name="t5"></a><a id="_69"></a>我的分析与总结</h3> 
<h4><a name="t6"></a><a id="_70"></a>为什么这个解法是最优的？</h4> 
<ol><li><strong>时间复杂度</strong>： 
  <ul><li>这个算法只需要遍历字符串一次，时间复杂度为 <span class="katex--inline"><span class="katex"><span class="katex-mathml"> 
        
         
          
          
            O 
           
          
            ( 
           
          
            n 
           
          
            ) 
           
          
         
           O(n) 
          
         
       </span><span class="katex-html"><span class="base"><span class="strut" style="height: 1em; vertical-align: -0.25em;"></span><span class="mord mathnormal" style="margin-right: 0.0278em;">O</span><span class="mopen">(</span><span class="mord mathnormal">n</span><span class="mclose">)</span></span></span></span></span>，非常高效。</li><li>判断一个字符是否为元音时，使用了 <code style="user-select: auto;">Set</code> 数据结构，其查找时间复杂度为 <span class="katex--inline"><span class="katex"><span class="katex-mathml"> 
        
         
          
          
            O 
           
          
            ( 
           
          
            1 
           
          
            ) 
           
          
         
           O(1) 
          
         
       </span><span class="katex-html"><span class="base"><span class="strut" style="height: 1em; vertical-align: -0.25em;"></span><span class="mord mathnormal" style="margin-right: 0.0278em;">O</span><span class="mopen">(</span><span class="mord">1</span><span class="mclose">)</span></span></span></span></span>。</li></ul> </li><li><strong>空间复杂度</strong>： 
  <ul><li>除了元音集合和几个变量外，没有额外的内存开销，空间复杂度为 <span class="katex--inline"><span class="katex"><span class="katex-mathml"> 
        
         
          
          
            O 
           
          
            ( 
           
          
            1 
           
          
            ) 
           
          
         
           O(1) 
          
         
       </span><span class="katex-html"><span class="base"><span class="strut" style="height: 1em; vertical-align: -0.25em;"></span><span class="mord mathnormal" style="margin-right: 0.0278em;">O</span><span class="mopen">(</span><span class="mord">1</span><span class="mclose">)</span></span></span></span></span>。</li></ul> </li><li><strong>逻辑清晰</strong>： 
  <ul><li>整个代码逻辑非常简单：一个遍历 + 两个变量的维护。即便是面对长度很大的字符串，也能高效运行，同时代码简洁易懂，便于维护。</li></ul> </li></ol> 
<h3><a name="t7"></a><a id="_78"></a>测试用例分析</h3> 
<p>在考试过程中，我也在心中模拟了一些测试用例来验证代码的正确性：</p> 
<ol><li><strong>普通场景</strong>： 
  <ul><li>输入：<code style="user-select: auto;">"hellothere"</code> 
    <ul><li>输出：<code style="user-select: auto;">2</code>（最长元音子串为 <code style="user-select: auto;">"e"</code>）。</li></ul> </li><li>输入：<code style="user-select: auto;">"beautifulday"</code> 
    <ul><li>输出：<code style="user-select: auto;">5</code>（最长元音子串为 <code style="user-select: auto;">"eau"</code>）。</li></ul> </li></ul> </li><li><strong>边界场景</strong>： 
  <ul><li>输入：<code style="user-select: auto;">"aaaaa"</code> 
    <ul><li>输出：<code style="user-select: auto;">5</code>（整个字符串都是元音）。</li></ul> </li><li>输入：<code style="user-select: auto;">"bcdfg"</code> 
    <ul><li>输出：<code style="user-select: auto;">0</code>（没有元音）。</li></ul> </li></ul> </li><li><strong>特殊场景</strong>： 
  <ul><li>输入：<code style="user-select: auto;">"AeiOU"</code> 
    <ul><li>输出：<code style="user-select: auto;">5</code>（元音子串为整个字符串，大小写不敏感）。<br> 这些测试用例覆盖了各种情况，验证了代码的正确性和鲁棒性。</li></ul> </li></ul> </li></ol> 
<h3><a name="t8"></a><a id="_95"></a>考试中的心得与总结</h3> 
<p>在考试中，我通过这道题认识到以下几点：</p> 
<ol><li><strong>需求分析的重要性</strong>： 
  <ul><li>拿到题目后，我第一时间认真拆解了需求，避免了方向性错误。这让我能快速确定高效的解决方案。</li></ul> </li><li><strong>选择合适的数据结构</strong>： 
  <ul><li>使用 <code style="user-select: auto;">Set</code> 存储元音字符，提高了判断效率。</li></ul> </li><li><strong>性能意识</strong>： 
  <ul><li>考虑到输入规模，我设计了时间复杂度为 <span class="katex--inline"><span class="katex"><span class="katex-mathml"> 
        
         
          
          
            O 
           
          
            ( 
           
          
            n 
           
          
            ) 
           
          
         
           O(n) 
          
         
       </span><span class="katex-html"><span class="base"><span class="strut" style="height: 1em; vertical-align: -0.25em;"></span><span class="mord mathnormal" style="margin-right: 0.0278em;">O</span><span class="mopen">(</span><span class="mord mathnormal">n</span><span class="mclose">)</span></span></span></span></span> 的解法，既高效又简洁。</li></ul> </li></ol> 
<p>最终，我的代码顺利通过了这道题，进一步让我体会到在面对复杂问题时，保持冷静、细致分析的重要性。<br> <img src="https://i-blog.csdnimg.cn/direct/5d77a952d23e4a40b1695bb4f90075dd.jpeg#pic_center" alt="在这里插入图片描述"></p>
                